﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using EFong.Services;
using EFong.ServicesInterface;

namespace EFong.Host
{
    public class HostMgt : IHostMgt
    {

        public int _Port = 0;
        public int Port {
            get {
                return (_Port == 0) ? 4444 : _Port;
            }
            set {

                if (this._Port != value) {
                    this._Port = value;

                    ResetStart();

                }
            }

        }

        TimeSpan sendTimeout = new TimeSpan(0, 5, 0);
        public TimeSpan Timeout// = new TimeSpan(0, 5, 0);
        {
            get { return sendTimeout; }
            set {
                if (sendTimeout != value) {
                    this.sendTimeout = value;
                    ResetStart();
                }
            }
        }

        private List<ServiceHost> serviceHostList = new List<ServiceHost>();

        public void Start(int port)
        {
            this._Port = port;
            ReallyStart();
        }

        public void Stop()
        {
            foreach (ServiceHost host in serviceHostList)
            {
                host.Abort();
            }
        }

        private void ReallyStart() {
            startBaseService();
            startBusinessService();
            startFinanceService();
            startMasterService();
            startReportService();
            startPrintService();
        }

        private void ResetStart()
        {
            Stop();
            ReallyStart();
        }


        private void startBaseService()
        {
            ServiceHost host = new ServiceHost(typeof(BaseService));

            Uri uri = new Uri(string.Format("http://127.0.0.1:{0}/{1}", _Port, "BaseService"));//从配置文件中读取服务的Url
            WSHttpBinding binding = new WSHttpBinding();
            //binding.ReliableSession.Enabled = true;
            binding.Security.Mode = SecurityMode.None;
            binding.SendTimeout = sendTimeout;
            binding.MaxReceivedMessageSize = 65536000;
            binding.ReaderQuotas.MaxArrayLength = 65536000;
            binding.ReaderQuotas.MaxStringContentLength = 65536000;

            host.AddServiceEndpoint(typeof(IBaseService), binding, uri);
            host.Open();

            serviceHostList.Add(host);
        }

        private void startFinanceService()
        {
            ServiceHost host = new ServiceHost(typeof(FinanceService));

            Uri uri = new Uri(string.Format("http://127.0.0.1:{0}/{1}", _Port, "FinanceService"));//从配置文件中读取服务的Url
            WSHttpBinding binding = new WSHttpBinding();
            //binding.ReliableSession.Enabled = true;
            binding.Security.Mode = SecurityMode.None;
            binding.SendTimeout = sendTimeout;
            binding.MaxReceivedMessageSize = 65536000;
            binding.ReaderQuotas.MaxArrayLength = 65536000;
            binding.ReaderQuotas.MaxStringContentLength = 65536000;

            host.AddServiceEndpoint(typeof(IFinanceService), binding, uri);
            host.Open();

            serviceHostList.Add(host);
        }

        private void startBusinessService()
        {
            ServiceHost host = new ServiceHost(typeof(BusinessService));

            Uri uri = new Uri(string.Format("http://127.0.0.1:{0}/{1}", _Port, "BusinessService"));//从配置文件中读取服务的Url
            WSHttpBinding binding = new WSHttpBinding();
            //binding.ReliableSession.Enabled = true;
            binding.Security.Mode = SecurityMode.None;
            binding.SendTimeout = sendTimeout;
            binding.MaxReceivedMessageSize = 65536000;
            binding.ReaderQuotas.MaxArrayLength = 65536000;
            binding.ReaderQuotas.MaxStringContentLength = 65536000;

            host.AddServiceEndpoint(typeof(IBusinessService), binding, uri);
            host.Open();

            serviceHostList.Add(host);
        }

        private void startMasterService()
        {
            ServiceHost host = new ServiceHost(typeof(MasterService));

            Uri uri = new Uri(string.Format("http://127.0.0.1:{0}/{1}", _Port, "MasterService"));//从配置文件中读取服务的Url
            WSHttpBinding binding = new WSHttpBinding();
            //binding.ReliableSession.Enabled = true;
            binding.Security.Mode = SecurityMode.None;
            binding.SendTimeout = sendTimeout;
            binding.MaxReceivedMessageSize = 65536000;
            binding.ReaderQuotas.MaxArrayLength = 65536000;
            binding.ReaderQuotas.MaxStringContentLength = 65536000;

            host.AddServiceEndpoint(typeof(IMasterService), binding, uri);
            host.Open();

            serviceHostList.Add(host);
        }

        private void startPrintService()
        {
            ServiceHost host = new ServiceHost(typeof(PrintService));
            host.Faulted += new EventHandler(host_Faulted);

            Uri uri = new Uri(string.Format("http://127.0.0.1:{0}/{1}", _Port, "PrintService"));//从配置文件中读取服务的Url
            WSHttpBinding binding = new WSHttpBinding();
            //binding.ReliableSession.Enabled = true;
            binding.Security.Mode = SecurityMode.None;
            binding.SendTimeout = sendTimeout;
            binding.MaxReceivedMessageSize = 65536000;
            binding.ReaderQuotas.MaxArrayLength = 65536000;
            binding.ReaderQuotas.MaxStringContentLength = 65536000;

            host.AddServiceEndpoint(typeof(IPrintService), binding, uri);
            host.Open();

            serviceHostList.Add(host);
        }

        void host_Faulted(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        private void startReportService()
        {
            ServiceHost host = new ServiceHost(typeof(ReportService));

            Uri uri = new Uri(string.Format("http://127.0.0.1:{0}/{1}", _Port, "ReportService"));//从配置文件中读取服务的Url
            WSHttpBinding binding = new WSHttpBinding();
            //binding.ReliableSession.Enabled = true;
            binding.Security.Mode = SecurityMode.None;
            binding.SendTimeout = sendTimeout;
            binding.MaxReceivedMessageSize = 65536000;
            binding.ReaderQuotas.MaxArrayLength = 65536000;
            binding.ReaderQuotas.MaxStringContentLength = 65536000;

            host.AddServiceEndpoint(typeof(IReportService), binding, uri);
            host.Open();

            serviceHostList.Add(host);
        }
    }
}
